package com.xintonghua.bussiness.ui.fragment.client;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.util.Log;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.MyUtils;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.BrithAdapter;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.MemberBirthdayBean;
import com.xintonghua.bussiness.bean.MemberBirthdayListBean;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.MyState;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：wanxianyi
 * 时间：2018/3/5.
 * 包名：com.xintonghua.bussiness.ui.fragment.client
 */

public class BirthuserActivity extends BaseActivity implements XRecyclerView.LoadingListener, getMemberBirthday {
    @BindView(R.id.tab_birth)
    TabLayout tabBirth;
    @BindView(R.id.lv_birth)
    XRecyclerView lvBirth;
    String[] title = new String[]{"今天生日", "本周生日", "本月生日"};
    BrithAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_birthuser);
        ButterKnife.bind(this);
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();

        setSimpleActionBar("会员生日");
        for (String str : title) {
            tabBirth.addTab(tabBirth.newTab().setText(str));
        }
        adapter = new BrithAdapter(this, new ArrayList<MemberBirthdayBean>());
        lvBirth.setAdapter(adapter);
        lvBirth.setLoadingListener(this);
        tabBirth.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Log.i("aaa", "position========>" + tab.getPosition() + "///" + tabBirth.getSelectedTabPosition());
                if (tab.getPosition() == 0) {
                    adapter.clear();
                    adapter.addAll(list.getThisDay());
                    adapter.setBirthdayType(0);
                } else if (tab.getPosition() == 1) {
                    adapter.clear();
                    adapter.addAll(list.getThisWeek());
                    adapter.setBirthdayType(1);
                } else if (tab.getPosition() == 2) {
                    adapter.clear();
                    adapter.addAll(list.getThisMonth());
                    adapter.setBirthdayType(2);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        RefreshUtils.initList(this, lvBirth, MyUtils.dip2px(this, 4), R.color.grey_bg);


        getMemberBirthday();
    }

    @Override
    public void onRefresh() {
        getMemberBirthday();
    }

    @Override
    public void onLoadMore() {
        RefreshUtils.onRefresh(adapter, lvBirth);
    }

    @Override
    public void getMemberBirthday() {
        HttpCent.getInstance(this).getMemberBirthdayList(BirthuserActivity.this, MyState.requestOne);
    }

    List<MemberBirthdayBean> todayM;
    List<MemberBirthdayBean> tBenM;
    List<MemberBirthdayBean> tMonthM;
    MemberBirthdayListBean list;

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne://获取会员生日列表
                try {
                    list = JsonUtil.getEntityByJsonString(data.toString(), MemberBirthdayListBean.class);
                    adapter.clear();
                    if (tabBirth.getSelectedTabPosition() == 0) {
                        adapter.addAll(list.getThisDay());
                    } else if (tabBirth.getSelectedTabPosition() == 1) {
                        adapter.addAll(list.getThisWeek());
                    } else if (tabBirth.getSelectedTabPosition() == 2) {
                        adapter.addAll(list.getThisMonth());
                    }
                    adapter.notifyDataSetChanged();
                    lvBirth.refreshComplete();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}

interface getMemberBirthday {
    //获取会员生日列表
    void getMemberBirthday();
}